package com.octopus.yangj.customized.controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.octopus.kernal.controller.AbstractCustomizedController;
import com.octopus.kernal.controller.SearchOperator;
import com.octopus.kernal.service.ConfigService;
import com.octopus.kernal.ui.element.BaseElement;
import com.octopus.yangj.customized.service.CustomizedStatisticsService;
import com.octopus.yangj.generated.controller.SearchEventCategoryCommand;
import com.octopus.yangj.generated.controller.SearchEventCommand;
import com.octopus.yangj.generated.controller.SearchStatisticsCommand;
import com.octopus.yangj.generated.controller.StatisticsValidator;
import com.octopus.yangj.generated.model.EventCategoryStruct;
import com.octopus.yangj.generated.model.EventStruct;
import com.octopus.yangj.generated.model.StatisticsStruct;
import com.octopus.yangj.generated.service.EventCategoryService;
import com.octopus.yangj.util.EventCategoryLevel;
import com.octopus.yangj.util.EventStatus;

@Controller
@RequestMapping("/statistics/x")
public class CustomizedStatisticsController extends AbstractCustomizedController<StatisticsStruct, SearchStatisticsCommand, Long>{
	
    @Autowired
    protected CustomizedStatisticsService statisticsService;
    
    @Autowired
    private EventCategoryService categoryService;
    
    @Autowired
    private StatisticsValidator statisticsValidator;
    
    @RequestMapping(value = "/query_by_category.o", method = RequestMethod.GET)
    public String queryByCategory(Model model, HttpServletRequest request) {
    	if(!SecurityUtils.getSubject().isPermitted("statistics:query")){
    		return "redirect:/common/unauthorized.o";
    	}
    	String pid = request.getParameter("pid");
    	List<StatisticsStruct> list = new ArrayList<StatisticsStruct>();
    	List<EventCategoryStruct> categories;
    	SearchEventCommand eventSearch = new SearchEventCommand();
    	SearchEventCategoryCommand categorySearch = new SearchEventCategoryCommand();
    	categorySearch.setEntity(new EventCategoryStruct());
    	categorySearch.setOperator(new EventCategoryStruct());
    	if(StringUtils.isBlank(pid)){
    		categorySearch.getEntity().setLevel(EventCategoryLevel.FIRST.getLevel());
    		categorySearch.getOperator().setLevel(SearchOperator.EQUAL.ordinal());
    		categories = categoryService.search(categorySearch);
    	}else{
    		EventCategoryStruct parent = categoryService.getEntity(Long.valueOf(pid));
    		String plabel = "<a href=\"query_by_category.o?pid=" + (null==parent.getParent()?"":parent.getParent().getId()) + "\"\">" + BaseElement.getTranslationValue(parent.getName()) + "</a>";
    		request.setAttribute("plabel", plabel);
    		categorySearch.getEntity().setParentId(Long.valueOf(pid));
    		categorySearch.getOperator().setParentId(Long.valueOf(SearchOperator.EQUAL.ordinal()));
    		categories = categoryService.search(categorySearch);
    	}
    	for(EventCategoryStruct category : categories){
        	eventSearch.setEntity(new EventStruct());
        	eventSearch.setOperator(new EventStruct());
    		String label = BaseElement.getTranslationValue(category.getName());
    		if(category.getLevel().equals(EventCategoryLevel.FIRST.getLevel())){
    			eventSearch.getEntity().setFirstCategoryId(category.getId());
    			eventSearch.getOperator().setFirstCategoryId(Long.valueOf(SearchOperator.EQUAL.ordinal()));
    			label = "<a href=\"query_by_category.o?pid=" + category.getId() + "\">" + label + "</a>";
    		}else if(category.getLevel().equals(EventCategoryLevel.SECOND.getLevel())){
    			eventSearch.getEntity().setSecondCategoryId(category.getId());
    			eventSearch.getOperator().setSecondCategoryId(Long.valueOf(SearchOperator.EQUAL.ordinal()));
    			label = "<a href=\"query_by_category.o?pid=" + category.getId() + "\">" + label + "</a>";
    		}else if(category.getLevel().equals(EventCategoryLevel.THIRD.getLevel())){
    			eventSearch.getEntity().setThirdCategoryId(category.getId());
    			eventSearch.getOperator().setThirdCategoryId(Long.valueOf(SearchOperator.EQUAL.ordinal()));
    			label = "<a href=\"#\">" + label + "</a>";
    		}
    		List<StatisticsStruct> l = statisticsService.search(eventSearch);
    		if(l != null && l.size() > 0){
	    		StatisticsStruct statistics = l.get(0);
	    		statistics.setLabel(label);
	    		list.add(statistics);
    		}
    	}
    	request.setAttribute("statistics", list);
    	return "customized/statistics/query_by_category";
    }
    
    @RequestMapping(value = "/query_by_status.o", method = RequestMethod.GET)
    public String queryByStatus(Model model, HttpServletRequest request) {
    	if(!SecurityUtils.getSubject().isPermitted("statistics:query")){
    		return "redirect:/common/unauthorized.o";
    	}
    	List<StatisticsStruct> list = new ArrayList<StatisticsStruct>();
    	List<EventStatus> statuses = EventStatus.getAllStatus();
    	SearchEventCommand eventSearch = new SearchEventCommand();
    	for(EventStatus status : statuses){
        	eventSearch.setEntity(new EventStruct());
        	eventSearch.setOperator(new EventStruct());
    		String label = statisticsValidator.getMessageSource().getMessage("common.single.select.event_status." + status.getStatus(), new Object[] { }, LocaleContextHolder.getLocale());
			eventSearch.getEntity().setStatus(status.getStatus());
			eventSearch.getOperator().setStatus(String.valueOf(SearchOperator.EQUAL.ordinal()));
    		List<StatisticsStruct> l = statisticsService.search(eventSearch);
    		if(l != null && l.size() > 0){
	    		StatisticsStruct statistics = l.get(0);
	    		statistics.setLabel(label);
	    		list.add(statistics);
    		}
    	}
    	request.setAttribute("statistics", list);
    	return "customized/statistics/query_by_status";
    }
    
    @RequestMapping(value = "/query_by_rate.o", method = RequestMethod.GET)
    public String queryByRate(Model model, HttpServletRequest request) {
    	if(!SecurityUtils.getSubject().isPermitted("statistics:query")){
    		return "redirect:/common/unauthorized.o";
    	}
    	List<StatisticsStruct> list = new ArrayList<StatisticsStruct>();
    	String[] rates = ConfigService.getInstance().getProperties("common.single.select.event_mark");
    	SearchEventCommand eventSearch = new SearchEventCommand();
    	for(String rate : rates){
        	eventSearch.setEntity(new EventStruct());
        	eventSearch.setOperator(new EventStruct());
    		String label = statisticsValidator.getMessageSource().getMessage("common.single.select.event_mark." + rate, new Object[] { }, LocaleContextHolder.getLocale());
			eventSearch.getEntity().setMark(Integer.valueOf(rate));
			eventSearch.getOperator().setMark(SearchOperator.EQUAL.ordinal());
    		List<StatisticsStruct> l = statisticsService.search(eventSearch);
    		if(l != null && l.size() > 0){
	    		StatisticsStruct statistics = l.get(0);
	    		statistics.setLabel(label);
	    		list.add(statistics);
    		}
    	}
    	request.setAttribute("statistics", list);
    	return "customized/statistics/query_by_rate";
    }
    
	@Override
	public String query(Model model, HttpServletRequest request, SearchStatisticsCommand searchCommand) {
		return null;
	}

	@Override
	public String search(Model model, HttpServletRequest request,
			SearchStatisticsCommand searchCommand) {
		return null;
	}

	@Override
	public String create(Model model, HttpServletRequest request, StatisticsStruct entity) {
		return null;
	}

	@Override
	public String saveCreate(Model model, HttpServletRequest request, StatisticsStruct entity,
			BindingResult errors) {
		return null;
	}

	@Override
	public String edit(Model model, HttpServletRequest request, Long id, StatisticsStruct entity) {
		return null;
	}

	@Override
	public String saveEdit(Model model, HttpServletRequest request, Long id,
			StatisticsStruct entity, BindingResult errors) {
		return null;
	}

	@Override
	public String view(Model model, HttpServletRequest request, Long id, StatisticsStruct entity) {
		return ("redirect:/statistics/query.o");
	}

	@Override
	public String delete(Model model, HttpServletRequest request, Long id) {
		return null;
	}


}
